﻿using System;
using System.Globalization;
using System.Windows;
using SKYPE4COMLib;
using Wra4Skype.Library;

namespace Wra4Skype
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly IViewModel viewModel;

        public MainWindow()
        {
            InitializeComponent();
            this.Closed += this.Window1_Closed;

            if (Properties.Settings.Default.FirstRun)
            {
                ShowMessage("This is the first time you are running this application.\r\nAfter you click Ok to close this dialog, open Skype and select 'Allow Access' to allow this application to interact with it!", MessageBoxButton.OK, MessageBoxImage.Information);
                Properties.Settings.Default.FirstRun = false;
                Properties.Settings.Default.Save();
            }

            this.viewModel = new ViewModel();
            this.viewModel.InvitationReceived += this.ViewModel_InvitationReceived;
            this.viewModel.UsersLoaded += this.ViewModel_UsersLoaded;
            this.viewModel.Connect();

            this.DataContext = this.viewModel;
        }

        private delegate void SimpleDispatcher();

        private void ViewModel_UsersLoaded(object sender, EventArgs e)
        {
            var simpleDelegate = new SimpleDispatcher(() => this.UserListBox.ItemsSource = this.viewModel.Users);
            this.Dispatcher.Invoke(simpleDelegate, null);
        }

        private void Window1_Closed(object sender, EventArgs e)
        {
            this.viewModel.Disconnect();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We catch exception to so the user here. We do want the most generic type of exception.")]
        private void ViewModel_InvitationReceived(object sender, EventArgs e)
        {
            var simpleDelegate = new SimpleDispatcher(() => this.HandleInvitationReceived());
            this.Dispatcher.Invoke(simpleDelegate, null);
        }

        private void HandleInvitationReceived()
        {
            try
            {
                Clipboard.SetText(this.viewModel.LastInvitation.Password);
                string message = string.Format(CultureInfo.CurrentUICulture, "User '{0}' invited you to help!\r\n\r\nWould you like to connect to his/her computer?\r\n\r\nPassword is '{1}' (It was copied to clipboard)", this.viewModel.LastInvitation.User.GetDisplayName(), this.viewModel.LastInvitation.Password);
                if (MessageBox.Show(this, message, "Wra4Skype", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    this.viewModel.AcceptLastInvitation();
                }
            }
            catch (Exception ex)
            {
                this.ShowException(ex);
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We catch exception to so the user here. We do want the most generic type of exception.")]
        private void InviteButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var selectedUser = (User)UserListBox.SelectedItem;
                this.viewModel.SendInvitation(selectedUser.Handle);
            }
            catch (Exception ex)
            {
                this.ShowException(ex);
            }
        }

        private void ShowException(Exception ex)
        {
            MessageBox.Show(this, ex.ToString(), "Wra4Skype", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private MessageBoxResult ShowMessage(string message, MessageBoxButton button, MessageBoxImage image)
        {
            return MessageBox.Show(this, message, "Wra4Skype", button, image);
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void AboutBoxButton_Click(object sender, RoutedEventArgs e)
        {
            new AboutWindow().ShowDialog();
        }
    }
}
